#include "include.h"
#include "api.h"

#if !TKET_CODE_OPT
typedef struct {
    u32 anov_thres;
    u8  sta;
    u8  stable_cnt;
    u16 cnt;
} te_cali_cb_t;

tk_cb_t tk_cb;
tk_cb_t te_cb;
#if USER_TKEY_MULT
tk_mult_t tk_mult_cb ;
#endif
te_cali_cb_t te_cali_cb;
u16 tkcnt_tmr[4];       //每个通路的实时TKCNT
u8  tk_ch_temp;
u8 cfg_user_tkey_mult_en = 0;//库里面有弱定义
#if USER_TKEY_DEBUG_EN
typedef struct {
    u16 tk_buf[16];
    u16 te_buf[16];
    char spp_buf[128];
    u8  tk_idx;
    u8  ch;
volatile u8 tk_print_sta;
    u32 ticks;
} spp_tkcb_t;
spp_tkcb_t spp_tkcb AT(.tkey_buf.debug);

AT(.rodata.tkey)
const char tkcnt_str[] = "[TK] State(%01x), TKBCNT(%03x):\n";
AT(.rodata.tkey)
const char tecnt_str[] = "[TE] State(%01x), TEBCNT(%03x):\n";

AT(.rodata.tkey)
const char tkbcnt_str[] = "TKBCNT: %x\n";
AT(.rodata.tkey)
const char tkpend_str[] = "TK ERR: %x\n";
AT(.rodata.tkey)
const char tebcnt_str[] = "TEBCNT: %x\n";
AT(.rodata.tkey)
const char tepend_str[] = "TE ERR: %x\n";

void spp_inpcon_tx(void)
{
//    int c_len = 0;
//
//    if (tk_pnd_cb.flag & BIT(0)) {
//        tk_pnd_cb.flag &= ~BIT(0);
//        c_len += sprintf(spp_tkcb.spp_buf+c_len, tepend_str, tk_pnd_cb.te_exp);
//    }
//    if (tk_pnd_cb.flag & BIT(1)) {
//        tk_pnd_cb.flag &= ~BIT(1);
//        c_len += sprintf(spp_tkcb.spp_buf+c_len, tebcnt_str, tk_pnd_cb.tebcnt);
//    }
//    if (tk_pnd_cb.flag & BIT(2)) {
//        tk_pnd_cb.flag &= ~BIT(2);
//        c_len += sprintf(spp_tkcb.spp_buf+c_len, tkpend_str, tk_pnd_cb.tk_exp);
//    }
//    if (tk_pnd_cb.flag & BIT(3)) {
//        tk_pnd_cb.flag &= ~BIT(3);
//        c_len += sprintf(spp_tkcb.spp_buf+c_len, tkbcnt_str, tk_pnd_cb.tkbcnt);
//    }
//    if (c_len) {
//        bt_spp_tx((uint8_t *)spp_tkcb.spp_buf, c_len);
//    }
}

//通过蓝牙SPP打印输出, 避免UART飞线干扰
void spp_tkcnt_tx(void)
{
//    u16 tk_buf[8], te_buf[8];
//    int c_len = 0, i;
//
//    if (spp_tkcb.tk_print_sta) {
//        memset(spp_tkcb.spp_buf, 0, sizeof(spp_tkcb.spp_buf));
//        memcpy(tk_buf, &spp_tkcb.tk_buf[(spp_tkcb.tk_print_sta - 1)*8], 16);
//        memcpy(te_buf, &spp_tkcb.te_buf[(spp_tkcb.tk_print_sta - 1)*8], 16);
//        spp_tkcb.tk_print_sta = 0;
//        if (tk_cb.ch != 0xff) {
//            c_len += sprintf(spp_tkcb.spp_buf + c_len, tkcnt_str, tkey_get_key(), (u16)(TKBCNT & 0xfff));
//            for (i = 0; i < 8; i++) {
//                c_len += sprintf(spp_tkcb.spp_buf + c_len, "%03x ", tk_buf[i] & 0xfff);
//            }
//            c_len += sprintf(spp_tkcb.spp_buf + c_len, "\n");
//        }
//#if USER_TKEY_INEAR
//        if (te_cb.ch != 0xff) {
//            c_len += sprintf(spp_tkcb.spp_buf + c_len, tecnt_str, tkey_is_inear(), (u16)(TEBCNT & 0xfff));
//            for (i = 0; i < 8; i++) {
//                c_len += sprintf(spp_tkcb.spp_buf + c_len, "%03x ", te_buf[i] & 0xfff);
//            }
//            c_len += sprintf(spp_tkcb.spp_buf + c_len, "\n");
//        }
//#endif
//        bt_spp_tx((uint8_t *)spp_tkcb.spp_buf, c_len);
//    }
}

void bsp_tkey_spp_tx(void)
{
    spp_tkcnt_tx();                     //输出TKCNT
//    if (tick_check_expire(spp_tkcb.ticks, 100)) {
//        spp_tkcb.ticks = tick_get();
//        spp_inpcon_tx();                //输出一些状态
//    }
}

#endif // USER_TKEY_DEBUG_EN


void bsp_tkey_var_init(void)
{
    memset(&tk_cb, 0, sizeof(tk_cb));
    memset(&te_cb, 0, sizeof(te_cb));
    memset(&te_cali_cb, 0, sizeof(te_cali_cb));
#if USER_TKEY_DEBUG_EN
    memset(&spp_tkcb, 0, sizeof(spp_tkcb));
    spp_tkcb.ch = 0xff;
#endif
    tk_cb.limit = 20;
    tk_cb.range_thresh = TKRTHRESH;
    tk_cb.ch = 0xff;
    tk_cb.fil_except = FIL_EXCEPT;
    tk_cb.bcnt_sfr = (psfr_t)(&TKBCNT);
    tk_cb.range_en = 0;                                 //软件range校准是否使能
    tk_cb.to_cnt = 1000;                                //8秒

    te_cb.te_flag = 1;
    te_cb.limit = 20;
    te_cb.range_thresh = TERTHRESH - 2;
    te_cb.ch = 0xff;
    te_cb.bcnt_sfr = (psfr_t)(&TEBCNT);
    te_cb.fil_except = EAR_FIL_EXCEPT;
    te_cb.range_en = 1;                             //软件range校准是否使能

    tk_ch_temp = 0xff;
#if USER_TKEY_MULT
    memset(&tk_cb, 0, sizeof(tk_mult_cb));
    tk_mult_cb.pb5_press_start = 1380;  //根据打印情况，掌握触摸和释放的规律，然后确定触摸和释放的区间，范围越大越灵敏
    tk_mult_cb.pb0_press_start = 1050;
    tk_mult_cb.pb1_press_start = 990;
    tk_mult_cb.pb2_press_start = 1100;

    tk_mult_cb.pb5_r_flag = 1;
    tk_mult_cb.pb0_r_flag = 1;
    tk_mult_cb.pb1_r_flag = 1;
    tk_mult_cb.pb2_r_flag = 1;
#endif // USER_TKEY_MULT
}

void bsp_tkey_init(void)
{
    bsp_tkey_var_init();
#if USER_TKEY
    int res;
    tkey_cfg_t tkey_cfg_v;
#if USER_TKEY_MULT
    if(cfg_user_tkey_mult_en == 1){
        memcpy(&tkey_cfg_v, &tkey_cfg_mult, sizeof(tkey_cfg_t));
    }else
#endif // USER_TKEY_MULT
    {
        memcpy(&tkey_cfg_v, &tkey_cfg_hard, sizeof(tkey_cfg_t));
    }
    bsp_tkey_channel_set((void *)&tkey_cfg_v);
    if (tk_ch_temp != 0xff) {
        tkey_cfg_v.key[tk_ch_temp] = &tkey_temp_cali_ch;
    }
    res = tkey_init((void *)&tkey_cfg_v, sys_cb.rtc_first_pwron);
    if (tk_cb.ch != 0xff) {
        if (!res || sys_cb.inbox_wken_flag || (RTCCON9 & BIT(4)) || (!(RTCCON9 & BIT(6)))) {
            delay_5ms(5);
            TKBCNT = tkcnt_tmr[tk_cb.ch];
        }
    }
    if (te_cb.ch != 0xff) {
        delay_5ms(4);
        TEBCNT = tkcnt_tmr[te_cb.ch];
    }
#endif // USER_TKEY
}

bool bsp_tkey_wakeup_en(void)
{
#if USER_TKEY
    if (tkey_cfg_hard.key[0] != NULL) {		//ch0跟wko同一个io
        return true;
    } else
#endif
    {
        return false;
    }
}
#if USER_TKEY_MULT
//AT(.com_text.tkey.isr_strr1)
//const char tkey_str[] = "ch[%d], tkcnt[%d]\n";
//AT(.com_text.tkey.isr_strr2)
//char tkey_tx_buf[20];
//AT(.com_text.tkey)
//void spp_tkey_tx(void)//spp打印
//{
//    memset(tkey_tx_buf, 0, sizeof(tkey_tx_buf));
//    sprintf(tkey_tx_buf, tkey_str, tkey_ch_cnt, (u16)(TKCNT & 0xFFFF));
//    bt_spp_tx((u8 *)tkey_tx_buf, 20);
//}

AT(.com_text.tkey)
void tkey_soft_kick(void)
{
    if(tk_mult_cb.tkey_mult_sta == 1)
    TKCON1 |= BIT(31);
}
// tkey_ch_cnt :0----->PB5
// tkey_ch_cnt :1----->PB0
// tkey_ch_cnt :2----->PB1
// tkey_ch_cnt :3----->PB2
AT(.com_text.tkey)
void tkey_ch_sel(void)
{
    if(tk_mult_cb.tkey_mult_sta == 1){
        tkey_ch_cnt++;
        TKCON1 = (TKCON1 & ~(0x3 << 14)) | (tkey_ch_cnt << 14);
        if(tkey_ch_cnt == 4){
            tkey_ch_cnt = 0;
        }
    }
}

AT(.com_text.tkey.isr_str)
const char tkey_str[] = "ch[%d], tkcnt[%d]\n";
AT(.com_text.tkeyyy)
const char pb5_1[] = "1";
AT(.com_text.tkeyyy)
const char pb5_0[] = "0";
AT(.com_text.tkey)
u8 mult_tkey_det(u8 ch, u16 tkcnt)
{
    volatile u16 tkcnt_det = tkcnt;
    if((ch == 0) && (tkcnt_det != 0)){                      //PB5按压阈值判断
//            my_printf(tkey_str, ch, tkcnt_det);           //打开则可以一直打印此通道的触摸值，同时看出按下和不按下的值变化情况
        if((tkcnt > tk_mult_cb.pb5_press_start)){
            tk_mult_cb.pb5_time++;
            tk_mult_cb.pb5_time_r = 0;
            if((tk_mult_cb.pb5_time == MULT_PRESS_TIMES) &&(tk_mult_cb.pb5_r_flag == 1)){
                tk_mult_cb.pb5_r_flag = 0;
                return 1;
            }
        }else{
                tk_mult_cb.pb5_time = 0;
                tk_mult_cb.pb5_time_r++;
                if(tk_mult_cb.pb5_time_r == MULT_RELCESE_TIMES){
                    tk_mult_cb.pb5_r_flag = 1;
                }
             }
    }
    if((ch == 1) && (tkcnt_det != 0)){                      //PB0按压阈值判断
//            my_printf(tkey_str, ch, tkcnt_det);           //打开则可以一直打印此通道的触摸值，同时看出按下和不按下的值变化情况
        if((tkcnt > tk_mult_cb.pb0_press_start)){
            tk_mult_cb.pb0_time++;
            tk_mult_cb.pb0_time_r = 0;
            if((tk_mult_cb.pb0_time == MULT_PRESS_TIMES) &&(tk_mult_cb.pb0_r_flag == 1)){
                tk_mult_cb.pb0_r_flag = 0;
                return 2;
            }
        }else{
                tk_mult_cb.pb0_time = 0;
                tk_mult_cb.pb0_time_r++;
                if(tk_mult_cb.pb0_time_r == MULT_RELCESE_TIMES){
                    tk_mult_cb.pb0_r_flag = 1;
                }
             }
    }
    if((ch == 2) && (tkcnt_det != 0)){                      //PB1按压阈值判断
//            my_printf(tkey_str, ch, tkcnt_det);           //打开则可以一直打印此通道的触摸值，同时看出按下和不按下的值变化情况
        if((tkcnt > tk_mult_cb.pb1_press_start)){
            tk_mult_cb.pb1_time++;
            tk_mult_cb.pb1_time_r = 0;
            if((tk_mult_cb.pb1_time == MULT_PRESS_TIMES) &&(tk_mult_cb.pb1_r_flag == 1)){
                tk_mult_cb.pb1_r_flag = 0;
                return 3;
            }
        }else{
                tk_mult_cb.pb1_time = 0;
                tk_mult_cb.pb1_time_r++;
                if(tk_mult_cb.pb1_time_r == MULT_RELCESE_TIMES){
                    tk_mult_cb.pb1_r_flag = 1;
                }
             }
    }
    if((ch == 3) && (tkcnt_det != 0)){                      //PB2按压阈值判断
//            my_printf(tkey_str, ch, tkcnt_det);           //打开则可以一直打印此通道的触摸值，同时看出按下和不按下的值变化情况
        if((tkcnt > tk_mult_cb.pb2_press_start)){
            tk_mult_cb.pb2_time++;
            tk_mult_cb.pb2_time_r = 0;
            if((tk_mult_cb.pb2_time == MULT_PRESS_TIMES) &&(tk_mult_cb.pb2_r_flag == 1)){
                tk_mult_cb.pb2_r_flag = 0;
                return 4;
            }
        }else{
                tk_mult_cb.pb2_time = 0;
                tk_mult_cb.pb2_time_r++;
                if(tk_mult_cb.pb2_time_r == MULT_RELCESE_TIMES){
                    tk_mult_cb.pb2_r_flag = 1;
                }
             }
    }
    return 0;
}

AT(.com_text.tkeyyy)
const char pb5_str[] = "-->pb5_pressed\n";
AT(.com_text.tkeyyy)
const char pb0_str[] = "----->pb0_pressed\n";
AT(.com_text.tkeyyy)
const char pb1_str[] = "-------->pb1_pressed\n";
AT(.com_text.tkeyyy)
const char pb2_str[] = "----------->pb2_pressed\n";

AT(.com_text.tkey)
void mult_tkey_chk(u8 res)
{
//    my_printf("[%d]", res);
	if(res == 1){
        printf(pb5_str);
	}
	if(res == 2){
        printf(pb0_str);
	}
	if(res == 3){
        printf(pb1_str);
	}
	if(res == 4){
        printf(pb2_str);
	}

}
#endif // USER_TKEY_MULT
#else
void bsp_tkey_init(void) {}
bool bsp_tkey_wakeup_en(void) {return false;}
#endif



